﻿package graph 
{
	/**
	 * ...
	 * @author s
	 */
	public class Node
	{
		public var userData:Object;

		private var _label:String;
		private var _weight:Number;
		private var _adjacentNodes:Vector.<Node>;
		private var _adjacentEdges:Vector.<Edge>;
		private var _adjacentEdgeHash:Object;
		private var _id:int;
		
		private static var _idMax:int = 1;
		
		public function Node(label:String, weight:Number)
		{
			_label = label;
			_weight = weight;
			_adjacentNodes = new Vector.<Node>;
			_adjacentEdges = new Vector.<Edge>;
			_adjacentEdgeHash = new Object;
			
			userData = null;
			
			_id = _idMax;
			_idMax++;
		}
		
		internal function addAdjacent(edge:Edge):void 
		{
			var node:Node;
			if (this != edge.dstNode) {
				node = edge.dstNode;
			}
			else {
				node = edge.srcNode;
			}
			_adjacentNodes.push(node);
			_adjacentEdges.push(edge);
			
			_adjacentEdgeHash[node.id] = edge;
		}

		/*
		 * getter, setter
		 */
		public function  get label():String 
		{
			return _label;
		}
		
		public function set weight(value:Number):void 
		{
			_weight = value;
		}
		
		public function get weight():Number 
		{
			return _weight;
		}
		
		public function get id():int
		{
			return _id;
		}
		
		public function adjacentNode(i:int):Node 
		{
			if(i >= 0 && i < _adjacentNodes.length){
				return _adjacentNodes[i];
			}
			else {
				return null;
			}
		}
		
		public function get numAdjacentNodes():int 
		{
			return _adjacentNodes.length;
		}
		
		public function adjacentEdge(i:int):Edge 
		{			
			if(i >= 0 && i < _adjacentEdges.length){
				return _adjacentEdges[i];
			}
			else {
				return null;
			}
		}
		
		public function get numAdjacentEdges():int 
		{
			return _adjacentEdges.length;
		}

		public function edgeTo(node:Node):Edge
		{
			if (_adjacentEdgeHash[node.id] != undefined) {
				return _adjacentEdgeHash[node.id];
			}
			else {
				return null;
			}
		}
	}
}